﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SnmpForm.APConfig
{
    public partial class ucWlanBasic : ucAPConfig
    {
        public ucWlanBasic()
        {
            InitializeComponent();
        }

        public override void LoadAp()
        {
            //无线信息基本信息
            //频道
            comboBoxChannal.DataSource = AP_DEFINE_DATA.WIRELESS_CHANNALS.Clone();

            //无线模式
            comboBoxMode.DataSource = AP_DEFINE_DATA.WIRELESS_MODE.Clone();

            //无线带宽
            comboBoxCWD.DataSource = AP_DEFINE_DATA.WIRELESS_CWD.Clone();
            Update();

            //
            bool bPrivilege = BusinessWrapper.User.Instance.Permision(
                BusinessWrapper.Types.Privilege.ManageAP.Wlan.WlanBasic.Save.permision);
            buttonSave.Visible = bPrivilege;
        }

        public override bool Update(bool bView = true)
        {
            //装载Ap配置
            DbWrapper.DbDataTypes.WirelessInfo info = m_ap.GetApWireless();

            if (bView)
            {
                int nIndex;
                ApConfigItem item = null;

                textBoxSSID.Text = info.ssid;

                item = AP_DEFINE_DATA.find(AP_DEFINE_DATA.WIRELESS_CHANNALS, info.channel, out nIndex);
                if (item != null)
                {
                    comboBoxChannal.SelectedIndex = nIndex;
                }

                item = AP_DEFINE_DATA.find(AP_DEFINE_DATA.WIRELESS_MODE, info.wirelessmode, out nIndex);
                if (item != null)
                {
                    comboBoxMode.SelectedIndex = nIndex;
                }

                item = AP_DEFINE_DATA.find(AP_DEFINE_DATA.WIRELESS_CWD, info.signalstrength, out nIndex);
                if (item != null)
                {
                    comboBoxCWD.SelectedIndex = nIndex;
                }

                checkBoxWirelessEnable.Checked = info.enable;
                checkBoxWDSEnable.Checked = info.wds;
            }
            else
            {
                info.ssid = textBoxSSID.Text;

                ApConfigItem item = null;
                item = (ApConfigItem)comboBoxChannal.SelectedValue;
                info.channel = (int)item.Value;

                item = (ApConfigItem)comboBoxMode.SelectedValue;
                info.wirelessmode = (string)item.Value;

                info.enable = checkBoxWirelessEnable.Checked;
                info.wds = checkBoxWDSEnable.Checked;
            }
            return true;
        }

        public override bool SyncToApCommit()
        {
            //将设置同步到Ap上
            string failure = "";
            bool isFalse = false;
            DbWrapper.DbDataTypes.WirelessInfo oldInfo, newInfo;
            oldInfo = m_ap.GetApWireless().Clone();
            Update(false);
            newInfo = m_ap.GetApWireless();
            if (oldInfo.ssid != newInfo.ssid)
            {
                if (m_ap.SetApSSID(newInfo.ssid) != true)
                {
                    failure += "ssid";
                    newInfo.ssid = oldInfo.ssid;
                    isFalse = true;
                }
            }
            if (oldInfo.channel != newInfo.channel)
            {
                if (m_ap.SetApPrimaryChannal(newInfo.channel) != true)
                {
                    failure += "channel ";
                    newInfo.channel = oldInfo.channel;
                    isFalse = true;
                }
            }
            if (oldInfo.wirelessmode != newInfo.wirelessmode)
            {
                if (m_ap.SetWirelessMode(newInfo.wirelessmode) != true)
                {
                    failure += "wirelessmode ";
                    newInfo.wirelessmode = oldInfo.wirelessmode;
                    isFalse = true;
                }
            }
            if (oldInfo.enable != newInfo.enable)
            {
                if (m_ap.SetWirelessOnBoot(newInfo.enable) != true)
                {
                    failure += "enable ";
                    newInfo.enable = oldInfo.enable;
                    isFalse = true;
                }
            }
            if (oldInfo.wds != newInfo.wds)
            {
                if (m_ap.SetWDS(newInfo.wds) != true)
                {
                    failure += "wirelessmode ";
                    newInfo.wds = oldInfo.wds;
                    isFalse = true;
                }
            }

            if (isFalse == true)
                MessageBox.Show(failure + "设置失败");
            return !isFalse;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (SyncToAp())
            {
                if (SaveToDb())
                {
                    NoticeConfigFormClose();
                }
            }
        }
    }
}
